// pages/trucks/truckList/truckList.js
const app = getApp()
var tab = require('../../../components/tabbar/tabbar.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    truckList: [],
    show: false, //弹出层
    status: '', //会员状态
    price: '',//充值金额
    page: 1,
    attestation: '' //认证状态
  },
  //充值
  top() {
    var that = this;
    console.log('点击充值');
    wx.request({
      url: app.globalData.url + 'api/v1/wechat/setorder',
      method: 'GET',
      data: {
        open_id: wx.getStorageSync('openid'),
        total_fee: this.data.price,
        body: '会员充值'
      },
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          let total_fee = parseInt(res.data.data.total_fee); //充值金额
          let body = res.data.data.body; //订单描述
          let out_trade_no = res.data.data.out_trade_no; //订单号
          wx.request({
            url: app.globalData.url + 'api/v1/wechat/wechat',
            method: 'POST',
            data: {
              open_id: wx.getStorageSync('openid'),
              total_fee: total_fee, //充值金额
              body: body, //订单描述
              out_trade_no: out_trade_no //订单号
            },
            success: res => {
              console.log(res)
              if(res.data.code == 200) {
                let param = res.data.data;
                wx.requestPayment({
                  nonceStr: param.nonceStr,
                  package: param.package,
                  paySign: param.paySign,
                  timeStamp: param.timeStamp,
                  signType: 'MD5',
                  success (res) {
                    console.log(res,'支付成功')
                    wx.showToast({
                      title: '支付成功'
                    })
                    that.setData({
                      show: false
                    })
                    // wx.setStorageSync('type', 1);
                    that.getMember()
                  },
                  fail (res) {
                    console.log(res)
                    wx.showToast({
                      title: '本次订单已取消',
                      icon: 'none',
                      mask: true
                    })
                  }
                })
              }
            },
            fail: err => {
              console.log(err)
            }
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //关闭弹窗
  onClose() {
    console.log('关闭弹窗')
    this.setData({ show: false });
  },
  //找车发货
  searchCar() {
    if(this.data.status == 200&&this.data.attestation == 2) {
      wx.navigateTo({
        url: '/pages/trucks/searchCar/searchCar'
      })
    }
    if(this.data.attestation != 1){
      if(this.data.status != 200){
        this.setData({
          show: true
        })
      }
    }else{
      wx.showToast({
        title: '请等待后台审核',
        icon: 'none',
        mask: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    tab.tabbar("tabBar", 0, this)//0表示第一个tabbar
    this.getList();
    this.getMember();
    this.getInfor();
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      page: 1
    })
    this.getList();
    this.getMember();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMember();
    this.getInfor();
  },

  /**
   * 页面上拉触底事件的处理函数
   * 上拉刷新
   */
  onReachBottom() {
    wx.showLoading({
      title: '加载中...',
      mark: true
    })
    setTimeout(() => {
      this.getList();
    }, 500);
  },
  //判断用户是否为会员
  getMember() {
    wx.request({
      url: app.globalData.url + 'api/v1/goods/goodslogin/isvip',
      method: 'GET',
      data: {
        open_id: wx.getStorageSync('openid')
      },
      success: res => {
        console.log(res)
        if(res.data.code == 200){
          wx.setStorageSync('id', res.data.data.data)
          this.getData();
          this.getInfor();
          this.setData({
            status: res.data.data.code
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //获取充值金额
  getData() {
    wx.request({
      url: app.globalData.url + 'api/v1/goods/goodslogin/vip',
      method: 'GET',
      data: {
        id: wx.getStorageSync('id')
      },
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            price: res.data.data.buy.data
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //个人信息
  getInfor() {
    wx.request({
      url: app.globalData.url + 'api/v1/goods/goodsresources/information',
      method: 'GET',
      data: {
        id: wx.getStorageSync('id')
      },
      success: res => {
        console.log(res)
        if(res.data.code == 200) {
          this.setData({
            attestation: res.data.data.examine
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  //货主列表
  getList() {
    wx.request({
      url: app.globalData.url + 'api/v1/goods/goodsresources/goodserlist',
      method: 'GET',
      data: {
        page: this.data.page,
        limit: 10
      },
      success: res => {
        console.log(res)
        if(res.data.code == 200){
           if(this.data.page > 1){
            if(res.data.data.data.info != ''){
              this.setData({
                truckList: this.data.truckList.concat(res.data.data.data.info),
                page: this.data.page + 1
              })
            }
          }else{
            this.setData({
              truckList: res.data.data.data.info,
              page: this.data.page + 1
            })
          }
        }else{
          setTimeout(() => {
            wx.showToast({
              title: '没有更多数据',
              icon: 'none',
              mask: true
            })
          }, 500);
        }
      },
      fail: err => {
        console.log(err)
      },
      complete: e => {
        console.log(e)
        wx.hideLoading();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})